Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → Global keyword

Python Functions

Global keyword

In Python, variables have scope. This means their accessibility is limited to specific parts of your code. The `global` keyword explicitly declares that a variable inside a function refers to the globally defined variable, rather than creating a new local variable with the same name. Let's explore this with detailed examples.

1. Understanding Scope without `global`

Without `global`, variables assigned within a function are considered local by default. They only exist within that function's scope.
Example of Scope without `global` in python x = 10 # Global variable def modify_x(): x = 20 # Local variable, shadows the global x print(f"Inside function: x = {x}") modify_x() print(f"Outside function: x = {x}")

Output

Inside function: x = 20 Outside function: x = 10
Notice that assigning `x = 20` inside `modify_x()` creates a new `x` that's only visible inside the function. The global `x` remains unchanged.

2. Using `global` to Modify Global Variables

To modify a global variable from within a function, you must use the `global` keyword.
Using `global` to Modify Global Variables x = 10 def modify_x_global(): global x # Declare x as global x = 20 print(f"Inside function: x = {x}") modify_x_global() print(f"Outside function: x = {x}")

Output

Inside function: x = 20 Outside function: x = 20
Here, the `global x` statement tells Python that any reference to `x` within `modify_x_global()` refers to the global `x`, allowing us to change its value.

3. `global` with Multiple Global Variables

You can declare multiple global variables within a single function using separate `global` statements.
`global` with Multiple Global Variables in Python a = 15 b = 25 def modify_globals(): global a, b # Declare both a and b as global a = 30 b = 40 print(f"Inside function: a = {a}, b = {b}") modify_globals() print(f"Outside function: a = {a}, b = {b}")

Output

Inside function: a = 30, b = 40 Outside function: a = 30, b = 40

4. `global` with Nested Functions

The `global` keyword's scope extends only to the function where it's declared. It doesn't affect nested functions unless those nested functions also explicitly declare the variable as global.
`global` with Nested Functions in Python x = 5 def outer(): global x x = 10 def inner(): # x is NOT global here unless declared as such within inner() print(f"Inside inner: x = {x}") #Accesses the globally modified x inner() print(f"Inside outer: x = {x}") outer() print(f"Outside all functions: x = {x}")

Output

Inside inner: x = 10 Inside outer: x = 10 Outside all functions: x = 10
If `inner()` needed to modify `x`, it would also require `global x` within its definition.

5. `global` and Mutable Objects

When dealing with mutable objects (like lists or dictionaries), you can modify them within a function without explicitly using `global`, as long as you don't reassign the entire object.
`global` and Mutable Objects in Python my_list = [1, 2, 3] def modify_list(): my_list.append(4) # Modifies the list in-place modify_list() print(my_list)

Output

[1, 2, 3, 4]

However, if you reassign `my_list` within the function, you are creating a new local variable:
`global` and Mutable Objects in Python my_list = [1, 2, 3] def reassign_list(): my_list = [4, 5, 6] # Creates a new local list reassign_list() print(my_list)

Output

[1, 2, 3]
In summary, the `global` keyword is crucial for explicitly stating your intention to work with global variables within functions. Misunderstanding scope and the use of `global` can lead to subtle bugs, so always use it carefully and judiciously. Favor other design patterns (like passing parameters) whenever possible to avoid unnecessary global dependencies.

Tutorials